using System; 
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine; 

namespace XFGameFramework.SkillSystem
{
    /// <summary>
    /// 技能配表
    /// </summary>
    [CreateAssetMenu(menuName ="XFKT/SkillSystem(技能配置)")]
    public class SkillsConfig : ScriptableObject
    {

        [HideInInspector]
        public int start_id = 0;

        [Header("显示名称")]
        public string displayName;

        [Tooltip("技能配置集合")]
        public List<SkillConfigData> skills = new List<SkillConfigData>();

        [HideInInspector]
        public string excel_path;

        [HideInInspector]
        public string type_script_guid;

        private Dictionary<int,SkillConfigData> skills_dic = new Dictionary<int, SkillConfigData>();

        /// <summary>
        /// 查询技能数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public SkillConfigData GetSkillConfigData(int id) {
             
            if(skills_dic.Count == 0) {
                foreach (var item in skills)
                {

                    if(item == null) continue;

                    if (skills_dic.ContainsKey(item.id))
                        throw new Exception(string.Format("技能id:{0}重复!",id));
                    skills_dic.Add(item.id, item);
                }
            }

            if (skills_dic.ContainsKey(id)) 
            {
                return skills_dic[id];
            }
             
            return null;
        }
        
        public void ClearCache()
        { 
            skills_dic.Clear();
        }
         
#if UNITY_EDITOR
        public Type GetDataType() 
        {
            if (!string.IsNullOrEmpty(type_script_guid)) 
            {
                string asset_path = AssetDatabase.GUIDToAssetPath(type_script_guid);
                MonoScript script = AssetDatabase.LoadAssetAtPath<MonoScript>(asset_path);
                if (script != null && script.GetClass() != null)
                {
                    Type type = script.GetClass();
                    if (type.IsSubclassOf(typeof(SkillConfigData)))
                        return type;
                }
            }
             
            return typeof(SkillConfigData);
        }

#endif

    }

}
